<?php

App::uses('AppController', 'Controller');

/**
 * Orders Controller
 *
 * @property Order $Order
 * @property PaginatorComponent $Paginator
 */
class OrdersController extends AppController
{

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->paginate = array('limit' => 10,'order'=>array('date'=>'DESC'));
        $listorder = $this->Paginator->paginate();
        $this->set('listorder',$listorder  );
        if($listorder !=null) {
			$this->Session->write('last', $listorder[0]['Order']['id']);
		}
    }

    public function unchoose()
    {

        $this->autoRender = false;
        $id = $this->request->data('id');
        if ($this->request->is('ajax')) {
            $this->Order->id = $id;
            if (!$this->Order->exists()) {
                return 'not exists';
            } else {
                if ($this->Order->save(array('id' => $id, 'status' => 1))) {
                    return 'ok';
                } else {
                    return 'error';
                }
            }
        }
    }

    public function updatestatus()
    {
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $data = $this->request->data;
            $sql = "UPDATE orders SET status = 2 WHERE id = {$data['id']}";
            $this->Order->query($sql);
            return 'ok';
        }
    }

    public function old()
    {
        $listorder = $this->Order->find('all', array('conditions' => array('status' => 2)));
        $this->set('listorder', $listorder);
    }

    public function renderComment($id = null)
    {
        if ($this->request->is('ajax')) {
            $this->loadModel('Comment');
            $cmts = $this->Comment->find('all', array('conditions' => array('orders_id' => $id)));
            $number_cmt = $this->Comment->find('count', array('conditions' => array('orders_id' => $id)));
            // debug($number_cmt);die;
            $this->set(compact('cmts', 'number_cmt'));

            $this->layout = 'ajax';
            $this->render('/elements/comment');
        }
//        if($this->request->is('post')) {
//            
//            $data = $this->request->data;
//            $data['Comment']['orders_id'] = $id;
//            $this->Comment->save($data);
//        }
    }

    /**
     *
     * @return json_encode
     */
    public function order()
    {
        $this->autoRender = false;
        date_default_timezone_set("Asia/Ho_Chi_Minh");
        $dateOrder = date("Y-m-d H:i:s");
        if ($this->request->is('ajax')) {
            $data = $this->request->data;
            $data['date'] = $dateOrder;
            $data['message'] = nl2br(trim(strip_tags($data['message'])));
            $data['song'] = strip_tags(trim($data['song']));
            $name = $data['sender'];
            $data['sender'] = strip_tags(trim($data['sender']));
            $data['receiver'] = strip_tags(trim($data['receiver']));


            if ($this->Order->save($data)) {
                $id = $this->Order->getLastInsertId();
                $data['id'] = $id;
                return json_encode($data);
            } else {
                return null;
            }
        }
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->Order->exists($id)) {
            throw new NotFoundException(__('Invalid order'));
        }
        $options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
        $this->set('order', $this->Order->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post')) {
            $this->Order->create();
            if ($this->Order->save($this->request->data)) {
                $this->Session->setFlash(__('The order has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The order could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Order->exists($id)) {
            throw new NotFoundException(__('Invalid order'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Order->save($this->request->data)) {
                $this->Session->setFlash(__('The order has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The order could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));
            $this->request->data = $this->Order->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Order->id = $id;
        if (!$this->Order->exists()) {
            throw new NotFoundException(__('Invalid order'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Order->delete()) {
            $this->Session->setFlash(__('The order has been deleted.'));
        } else {
            $this->Session->setFlash(__('The order could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }

}
